package com.lanou3g.k12.web.controller;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.lanou3g.k12.entity.Notice;
import com.lanou3g.k12.service.NoticeService;
import com.lanou3g.k12.utils.K12Result;
import com.lanou3g.k12.utils.K12ResultUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.*;

@RestController
public class NoticeController {
    @Autowired
    private NoticeService noticeService;

    //    根据id删除
    @DeleteMapping("/notice/{id}")
    public K12Result deleteById(@PathVariable long id) {
        try {
            noticeService.deleteById(id);
            return K12ResultUtils.ok();
        } catch (Exception e) {
            e.printStackTrace();
            return K12ResultUtils.error(533);
        }
    }

    //    根据id们批量删除
    @DeleteMapping("/notices/{ids}")
    public K12Result deleteByIds(@PathVariable long[] ids) {
        try {
            noticeService.deleteByIds(ids);
            return K12ResultUtils.ok();
        } catch (Exception e) {
            e.printStackTrace();
            return K12ResultUtils.error(533);
        }
    }

    //    获取公告列表
    @GetMapping("/notices")
    public K12Result findAll(String keyword,
                             @RequestParam(value = "page", defaultValue = "1") Integer page,
                             @RequestParam(value = "size", defaultValue = "30") Integer size) {

        try {
            Page<Object> objects = PageHelper.startPage(page, size);
            List<Notice> noticeList;
            if (keyword != null && !keyword.trim().equals("")) {
                noticeList = noticeService.findByKeyword(keyword, page, size);
            } else {
                noticeList = noticeService.findAll(page, size);
            }
            return K12ResultUtils.ok(objects);
        } catch (Exception e) {
            e.printStackTrace();
            return K12ResultUtils.error(533);
        }
    }

    //    根据id修改
    @PutMapping("/notice/{id}")
    public K12Result updateById(@PathVariable Long id, Notice notice) {
        try {
            noticeService.updateById(id,notice);
            return K12ResultUtils.ok();
        } catch (Exception e) {
            e.printStackTrace();
            return K12ResultUtils.error(533);
        }
    }

    // 新建一条公告
    @PostMapping("/notice")
    public K12Result insertNotice(Notice notice,Integer sort) {
        System.out.println(notice);
        try {
            noticeService.insertNotice(notice,sort);
            return K12ResultUtils.ok();
        } catch (Exception e) {
            e.printStackTrace();
            return K12ResultUtils.error(533);
        }

    }

    // 根据id查找
    @GetMapping("/notice/{id}")
    public K12Result selectNoticeById(@PathVariable Long id) {
        System.out.println(id);
        try {
            Notice notices = noticeService.selectNoticeById(id);
            System.out.println(notices);
            return K12ResultUtils.ok(notices);
        } catch (Exception e) {
            e.printStackTrace();
            return K12ResultUtils.error(533);
        }

    }
}


